package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysProjectInformationMappingDaoI;
import sy.model.SysProjectInformationMapping;

@Repository("sysProjectInformationMappingDao")
public class SysProjectInformationMappingDaoImpl implements SysProjectInformationMappingDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysProjectInformationMapping sysProjectInformationMapping) {
		return getCurrentSession().save(sysProjectInformationMapping);//直接用
		
	}

	@Override
	public void remove(SysProjectInformationMapping sysProjectInformationMapping) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysProjectInformationMapping);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysProjectInformationMappingDaoI#update(sy.model.SysProjectInformationMapping)
	 */
	@Override
	public void update(SysProjectInformationMapping sysProjectInformationMapping) {
		// TODO Auto-generated method stub
		getCurrentSession().update(sysProjectInformationMapping);
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysProjectInformationMappingDaoI#list()
	 */
	@Override
	public List<SysProjectInformationMapping> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysProjectInformationMapping").list();
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysProjectInformationMappingDaoI#getSysProjectInformationMappingById(java.lang.String)
	 */
	@Override
	public SysProjectInformationMapping getSysProjectInformationMappingById(
			String id) {
		// TODO Auto-generated method stub
		String hql = "from SysProjectInformationMapping where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysProjectInformationMapping> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see sy.dao.SysProjectInformationMappingDaoI#listSysProjectInformationMappingByKey(java.lang.String)
	 */
	@Override
	public List<SysProjectInformationMapping> listSysProjectInformationMappingByKey(
			String key) {
		// TODO Auto-generated method stub
		String hql = "from SysProjectInformationMapping where sysInformation.id like :key or sysProject.id like :key";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("key", "%"+key+"%");
		List<SysProjectInformationMapping> list = query.list();
		return list;
	}

	@Override
	public Serializable saveSysProjectInformationMappings(//批量增加实体
			List<SysProjectInformationMapping> sysProjectInformationMappings) {
		// TODO Auto-generated method stub
		Session session = this.getCurrentSession();
		SysProjectInformationMapping projInfo = null;
		for(int i = 0; i<sysProjectInformationMappings.size(); i++){
			projInfo = sysProjectInformationMappings.get(i);
			session.save(projInfo);
			if (i % 10 == 0) {  
				session.flush();  
				session.clear();  
			}  
		}
		return null;
	}

	@Override
	public void removeByInformation(String informationId) {
		// TODO Auto-generated method stub
		String hql = "delete from SysProjectInformationMapping where sysInformation.id = :informationId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("informationId", informationId);
		query.executeUpdate();
	}

}
